﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    static DataTable dt_sta = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   
            //绑定显示登录者的大队或支队编号的重点单位
            string DepartmentID=string.Empty;
            DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            //DepartmentID = "1301000";

            ViewState.Add("DepartmentID", DepartmentID);
            gvBind(DepartmentID);

            DBCls db = new DBCls();
            DataTable dt = new DataTable();

            string p4 = DepartmentID.Substring(3, 1).ToString();
            string p5= DepartmentID.Substring(4,1).ToString();
            if (p4 == "0")
            {
                //总队
            }
            else if (p5 == "0")
            {
                //支队
            }
            else if (p5 == "2")
            {
                //大队
            }

            //绑定支队和大队列表
            string sqlzhidui = "select departmentid,fullname from Account_Departments where departmentid like'%000' and departmentid <> 1300000";
            dt.Clear();
            dt = db.QueryDataTable(sqlzhidui, "zhidui");
            if (dt.Rows.Count > 0)
            {
                //支队
                ddlZD.Items.Clear();
                ddlZD.DataSource = dt;
                ddlZD.DataTextField = "fullname";
                ddlZD.DataValueField = "departmentid";
                ddlZD.DataBind();
                //大队
                ddlZD_SelectedIndexChanged(sender, e);
            }
            
            //绑定重点单位类型
            dt.Clear();
            dt = db.QueryDataTable("select KeyUnitTypeID,KeyUnitTypeDescript from KeyUnitType", "KeyUnitType");
            if (dt.Rows.Count > 0)
            {
                ddlUnitType.Items.Clear();
                ddlUnitType.DataSource = dt;
                ddlUnitType.DataTextField = "KeyUnitTypeDescript";
                ddlUnitType.DataValueField = "KeyUnitTypeID";
                ddlUnitType.DataBind();
            }
            //重点单位类型对照表

            string sql = "select KeyUnitTypeDescript,KeyUnitTypeName from KeyUnitType";
            SqlDataReader sqlReader = db.SqlReader(sql);

            DataTable Dt = new DataTable();
            DataRow dr;
            Dt.Columns.Add("KeyUnitTypeDescript", typeof(string));    //第几类
            Dt.Columns.Add("KeyUnitTypeName", typeof(string));   //类型全称
            


            while (sqlReader.Read())
            {
                dr = Dt.NewRow();
                dr[0] = sqlReader[0].ToString();
                dr[1] = sqlReader[1].ToString();
                
                Dt.Rows.Add(dr);
            }
            sqlReader.Close();
            this.DataList1.DataSource = new DataView(Dt);
            this.DataList1.DataBind();

        }
    }
    protected void ddlZD_SelectedIndexChanged(object sender, EventArgs e)
    {
        //绑定大队 消防科
        string departmentid = ddlZD.SelectedValue.ToString();
        departmentid = departmentid.Substring(0, 4);
        string sqldadui = "select departmentid,fullname,shortname from Account_Departments where departmentid like'" + departmentid + "%' and ( shortname like'%大队' or shortname like'%消防科')";
        DBCls db = new DBCls();
        DataTable dt = new DataTable();
        dt = db.QueryDataTable(sqldadui, "dadui");
        if (dt.Rows.Count > 0)
        {
            ddlDD.Items.Clear();
            ddlDD.DataSource = dt;
            ddlDD.DataTextField = "fullname";
            ddlDD.DataValueField = "departmentid";
            ddlDD.DataBind();
        }
        //gv没有数据时刷新后不显示“没有数据”
        //没有数据时重新绑定gv
    }
    void gvBind(string DepartmentID)
    {
        DBCls db = new DBCls();
        string sql = "select PerformanceID,KeyUnitName,PerformanceState from Performance where DepartmentID='" + DepartmentID + "'";
        try
        {
            dt_sta = db.QueryDataTable(sql, "Performance");
            
        }
        catch(Exception ex)
        {
            Response.Write("<script language=javascript>alert('databing错误" + ex.Message + "！')</script>");
            return;
        }
        //如果有数据
        if (dt_sta.Rows.Count > 0)
        {
            this.GridView1.DataSource = dt_sta;
            this.GridView1.DataBind();
        }
        //如果没有数据
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Clear();

            dt.Columns.Add("PerformanceID");
            dt.Columns.Add("KeyUnitName");
            dt.Columns.Add("PerformanceState");
           
            dt.Columns.Add("查看");
            dt.Columns.Add("编辑");
            dt.Columns.Add("删除");

            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
            }

            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            //this.GridView1.HeaderStyle.Height = Unit.Pixel(10);
            // this.GridView1.RowStyle.Height = Unit.Pixel(7);
            int columnCount = dt.Columns.Count;
            //移除单元格数据
            GridView1.Rows[0].Cells.Clear();
            //添加新单元格
            GridView1.Rows[0].Cells.Add(new TableCell());
            //合并单元格
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            //设置单元格的显示的信息
            GridView1.Rows[0].Cells[0].Text = "没有记录";
            // 设置单元格的样式，居中显示
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
            GridView1.Rows[0].Cells[0].Style.Add("color", "red");
            GridView1.Rows[0].ForeColor = System.Drawing.Color.Red;
            //隐去 ID
            //  GridView1.Columns[0].Visible = false;
            //this.GridView1.DataSource = dt;
            //this.GridView1.DataBind();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int num = Convert.ToInt32(e.CommandArgument);//得行号
            string id = this.GridView1.DataKeys[num]["PerformanceID"].ToString();//得主键值

            DBCls db = new DBCls();

            string s = e.CommandName.ToString();
            //查看
            if (s == "Select")
            {
                Response.Redirect("KeyUnitView.aspx?id=" + id + "");
            }
            //修改
            if (s == "Edit")
            {
                Response.Redirect("KeyUnitEdit.aspx?id=" + id + "");
            }
            //删除
            if (s == "Delete")
            {
                string sqlPerformance = "delete Performance where PerformanceID='" + id + "'";
                string sqlPerformancePic = "delete PerformancePic where PerformanceID='" + id + "'";

                try
                {
                    if (db.ExcuteSql2(sqlPerformance,sqlPerformancePic))
                    {

                        Response.Write("<script language=javascript>alert('删除成功')</script>");
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('删除失败！')</script>");
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("<script language=javascript>alert('删除失败！" + ex.Message + "')</script>");
                }

                //重新绑定
                this.gvBind(ViewState["DepartmentID"].ToString());
               
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('操作失败！gridview-rowcommand错误！" + ex.Message + "！')</script>");
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("class", "text");
        }
        int i;
        int count = this.GridView1.Rows.Count;
        //  string id = this.GridView1.DataKeys[e.Row.RowIndex]["ProductID"].ToString();
        for (i = 0; i <= count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                //添加删除提示
                if (e.Row.Cells.Count > 5)
                {
                    e.Row.Cells[5].Attributes.Add("onclick", "return confirm('确定删除吗？');");
                }
            }
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = this.GridView1.DataKeys[e.NewEditIndex].Values[0].ToString();
        ////清空后赋值
        //this.ViewState.Clear();
        //ViewState.Add("PerformanceID", id.ToString());
        ////设置可编辑
        //this.txtUserName.Enabled = true;
        //this.ddlCompany.Enabled = true;
        //this.rblSex.Enabled = true;
        //this.rblPost.Enabled = true;
        //this.txtAge.Enabled = true;
        //this.txtTele.Enabled = true;
        //this.txtMoblie.Enabled = true;
        //this.txtAddress.Enabled = true;
        //this.txtEmail.Enabled = true;
        //this.txtUserRemark.Enabled = true;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataSource = dt_sta;
        this.GridView1.DataBind();
    }

    //查询
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string DepartmentID = ddlDD.SelectedValue.ToString();
        string type = ddlUnitType.SelectedValue.ToString();
        DBCls db = new DBCls();
        string sql = "select PerformanceID,KeyUnitName,PerformanceState from Performance where DepartmentID='" + DepartmentID + "' and KeyUnitTypeID ='" + type + "'";
        try
        {
            dt_sta = db.QueryDataTable(sql, "Performance");

        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('databing错误" + ex.Message + "！')</script>");
            return;
        }
        //如果有数据
        if (dt_sta.Rows.Count > 0)
        {
            this.GridView1.DataSource = dt_sta;
            this.GridView1.DataBind();
        }
        //如果没有数据
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Clear();

            dt.Columns.Add("PerformanceID");
            dt.Columns.Add("KeyUnitName");
            dt.Columns.Add("PerformanceState");

            dt.Columns.Add("查看");
            dt.Columns.Add("编辑");
            dt.Columns.Add("删除");

            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
            }

            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            //this.GridView1.HeaderStyle.Height = Unit.Pixel(10);
            // this.GridView1.RowStyle.Height = Unit.Pixel(7);
            int columnCount = dt.Columns.Count;
            //移除单元格数据
            GridView1.Rows[0].Cells.Clear();
            //添加新单元格
            GridView1.Rows[0].Cells.Add(new TableCell());
            //合并单元格
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            //设置单元格的显示的信息
            GridView1.Rows[0].Cells[0].Text = "没有记录";
            // 设置单元格的样式，居中显示
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
            GridView1.Rows[0].Cells[0].Style.Add("color", "red");
            GridView1.Rows[0].ForeColor = System.Drawing.Color.Red;
            //隐去 ID
            //  GridView1.Columns[0].Visible = false;
            //this.GridView1.DataSource = dt;
            //this.GridView1.DataBind();
        }
    }
}
